import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.FileReader;
import javax.swing.*;

public class Week07 extends JFrame {

	private JPanel mainPane;
	private JMenuBar menuBar;
	private JMenu menu1;
	private JMenuItem item1, item2;
	private JLabel currentFileLabel;
	private JButton doSort;
	private JTextField selectedFile;
	private	JFileChooser chooser = new JFileChooser();


	public Week07() {

		final int WINDOW_WIDTH = 600;
		final int WINDOW_HEIGHT = 180;


		mainPane = new JPanel();
		menuBar = new JMenuBar();

		// Set up and add menu bar
		menuBar.setPreferredSize(new Dimension(450,20));
		setJMenuBar(menuBar);

		// Set up the menus
		menu1 = new JMenu("File");
		item1 = new JMenuItem("Open File");
		item2 = new JMenuItem("Exit Program");
		menu1.add(item1);
		menu1.add(item2);
		menuBar.add(menu1);
		item1.addActionListener(new FileOpenListener());
		item2.addActionListener(new ExitProgramListener());

		// Set up labels
		currentFileLabel = new JLabel("Currently Reading: ");
		// Set up buttons
		doSort = new JButton("Sort and List");
		doSort.addActionListener(new doSortListener());


		selectedFile = new JTextField("Select a File");
		selectedFile.setEditable(false);

		// Setup the content pane
		mainPane.setLayout(null);
		mainPane.add(currentFileLabel);
		currentFileLabel.setBounds(25,25,190,18);

		mainPane.add(selectedFile);
		selectedFile.setBounds(25,50,530,20);

		mainPane.add(doSort);
		doSort.setBounds(25,76,200,20);
		doSort.setVisible(false);



		setContentPane(mainPane);
		setTitle("Week 07 Prototype");
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setVisible(true);
	}

	class FileOpenListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int returnVal = chooser.showOpenDialog(mainPane);

			if (returnVal == JFileChooser.APPROVE_OPTION) {

				if (!chooser.getSelectedFile().exists()){
					JOptionPane.showMessageDialog(null, "File does not exist! Please choose another file.");
				}
				else if (chooser.getSelectedFile().exists()) {
					selectedFile.setText(chooser.getSelectedFile().getPath());
					doSort.setVisible(true);
				}
			}
		}
	}

	class ExitProgramListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	class doSortListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				CSVReader reader = new CSVReader(new FileReader(chooser.getSelectedFile()));
				String [] nL= reader.readNext(); //read and discard title line
				long stuID;
				String stuName;
				String stuOverall;
				int[] stuScores;
				Student[] sA1 = new Student[999];
				int nsA=0;
				
				while ((nL = reader.readNext()) != null) {
					// nL[] is an array of values from the line
					if (nL.length <4) {
						break;
					}
					stuID = Long.parseLong(nL[0]);
					stuName = nL[1];
					stuOverall = nL[2];
					stuScores = new int[nL.length-3];
					for (int i = 0; i<stuScores.length; i++) {
						stuScores[i] = Integer.parseInt(nL[i+3]);
					}
					sA1[nsA++] = new Student(stuID,stuName,stuOverall,stuScores);
				}
				Student[] sA = new Student[nsA];
				for (int i=0; i< nsA; i++) sA[i]=sA1[i];
				doQuickSort(sA);
				printStudents(sA);
			}
			catch (IOException ex) {
				System.out.println("Some I/O error");
			}
			catch (Exception ex) {
				System.out.println("Some random error");
			}
		}
	}
	public static void doQuickSort(Student[] x) {
		// stub: fill out code here
	}
	
	public static void printStudents(Student[] x) {
		for (int i=0; i<x.length; i++) {
			System.out.println(x[i]);
		}
	}

	public static void main(String[] args) {
		Week07 object = new Week07();
	}
}
